package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.share.wxapi.OpenLoginListener;
import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.common.Constants;
import com.viphuli.app.tool.common.MyApp;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 登录
 * 
 * @author li.li
 *
 */
public class AccountLoginFragment extends AccountVCodeFragment {

	@Bind(R.id.id_account_login_btn)
	protected TextView loginBtn;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_login;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {
	}

	/**
	 * 手机验证码登陆
	 */
	@OnClick(R.id.id_account_login_btn)
	public void login() {

		String phone = phoneText.getText().toString();
		String vCode = vcodeText.getText().toString();

		if (StringUtils.isBlank(phone)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_phone_number), Toast.LENGTH_SHORT).show();
			return;
		}

		if (StringUtils.isBlank(vCode)) {
			Toast.makeText(getActivity(), getString(R.string.tip_input_vcode), Toast.LENGTH_SHORT).show();
			return;
		}

		RequestParams params = new RequestParams();
		params.put("phone_number", phone);
		params.put("v_code", vCode);

		ApiRequest.accountLogin.request(this, params);

	}

	/**
	 * 微信登陆
	 */
	@OnClick(R.id.id_account_wechat_login_btn)
	public void wechatLogin() {
		String appId = MyApp.getInstance().getString(R.string.weichat_app_id);
		String appSecret = MyApp.getInstance().getString(R.string.weichat_app_secret);
		ShareUtils.loginWechat(getActivity(), appId, appSecret, new OpenLoginListener() {

			@Override
			public void onSuccess(String accessToken, String openId) {

				if (StringUtils.isNotBlank(accessToken) && StringUtils.isNotBlank(openId)) {

					RequestParams params = new RequestParams();
					params.put("openid", openId);
					params.put("access_token", accessToken);
					params.put("login_type", 1);
					ApiRequest.accountWechatLogin.request(AccountLoginFragment.this, params);

				}

			}
		});
	}

	public TextView getLoginTtn() {
		return loginBtn;
	}

	public static void go(Activity act) {
		MyPageHelper.accountLogin.showMyPage(act);
	}

	public static void go(Activity act, int resultCode) {
		MyPageHelper.accountLogin.showMyPage(act, resultCode);
	}

	public static void goWithConfirm(final Activity act) {

		ViewUtils.confirm(act, act.getString(R.string.tip_need_login), new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				MyPageHelper.accountLogin.showMyPage(act);
			}
		});
	}

	public static void goWithConfirm(final Activity act, String msg) {

		ViewUtils.confirm(act, msg, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				MyPageHelper.accountLogin.showMyPage(act);
			}
		});
	}

	public static void goWithConfirmResult(final Activity act, String msg) {

		ViewUtils.confirm(act, msg, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				MyPageHelper.accountLogin.showMyPage(act, Constants.ACTIVITY_REQUEST_REFREASH_CODE);
			}
		});
	}

}
